package cn.edu.zzuli.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.edu.zzuli.web.Constant;
import cn.edu.zzuli.web.bean.User;
import cn.edu.zzuli.web.controller.model.UserForm;
import cn.edu.zzuli.web.dao.base.PageView;
import cn.edu.zzuli.web.dao.base.QueryResult;

/**
 * 用户操作控制器
 * @author caohua
 */
@Controller
public class UserController extends AbstractController{
	
	public UserController() {}
	
	/**
	 * 用户列表页面
	 */
	@RequestMapping(value = "/user/list", method = RequestMethod.GET)
	public String userList(HttpServletRequest request, HttpServletResponse response) {
		int currentPage = 0;
		String c = request.getParameter("currentPage");
		if(!StringUtils.isBlank(c) && StringUtils.isNumeric(c))
			currentPage = Integer.parseInt(c);
		
		if(currentPage <= 0) 
			currentPage = 1;
		
		int record = this.getUserDao().getTotalUser();
		List<User> list = this.getUserDao().getUserByPage(Constant.PAGE_SIZE, currentPage);
		
		PageView<User> pageView = new PageView<User>(Constant.PAGE_SIZE, currentPage);
		
		QueryResult<User> qr = new QueryResult<User>();
		qr.setResultlist(list);
		qr.setTotalrecord(record);
		
		pageView.setQueryResult(qr);
		request.setAttribute("pageView", pageView);

		return "/user/list";
	}

	/**
	 * 用户增加页面
	 */
	@RequestMapping(value = "/user/add", method = RequestMethod.GET)
	public String userAddUI() {
		return "/user/add";
	}
	
	/**
	 * 用户增加操作
	 */
	@RequestMapping(value = "/user/add", method = RequestMethod.POST)
	public String userAdd(HttpServletRequest request, HttpServletResponse response, UserForm form) throws Exception{
		User tuser = this.getUserDao().getUserByUsername(form.getUsername(),form.getPassword());
		if(tuser.getUsername() != null && tuser.getPassword() != null){
			request.setAttribute("inputerror", "用户名存在");
			return "/user/add";
		}
		
		User user = new User();
		BeanUtils.copyProperties(user, form);
		this.getUserDao().insertUserWithType(user);
		
		User userSecurity = this.getUserDao().getUserByUsername(user.getUsername(), user.getPassword());
		this.getUserDao().insertUserSecurity(userSecurity,form.getSecurity());
		
		return "redirect:/user/list.html";
	}
	
	/**
	 * 用户修改页面
	 */
	@RequestMapping(value = "/user/edit", method = RequestMethod.GET)
	public String userEditUI(HttpServletRequest request, HttpServletResponse response,int id) {
		User user = this.getUserDao().getUserById(id);
		if(user.getUsername() == null && user.getPassword() == null)
			return "redirect:/user/list.html";
		
		int roleId = this.getUserDao().getUserSecurity(user.getId());
		request.setAttribute("roleId", roleId);
		request.setAttribute("user", user);
		return "/user/edit";
	}
	
	/**
	 * 用户修改页面
	 */
	@RequestMapping(value = "/user/edit", method = RequestMethod.POST)
	public String userEdit(HttpServletRequest request, HttpServletResponse response, UserForm form) throws Exception {
		User user = new User();
		BeanUtils.copyProperties(user, form);
		this.getUserDao().updateUser(user);
		
		this.getUserDao().updateUserSecurity(user, form.getSecurity());
		return "redirect:/user/list.html";
	}
	
	/**
	 * 用户查看页面
	 */
	@RequestMapping(value = "/user/view", method = RequestMethod.GET)
	public String userView(HttpServletRequest request, HttpServletResponse response,int id) {
		User user = this.getUserDao().getUserById(id);
		if(user.getUsername() == null && user.getPassword() == null)
			return "redirect:/user/list.html";
			
		request.setAttribute("user", user);
		return "/user/view";
	}
	
	/**
	 * 用户修改密码页面
	 */
	@RequestMapping(value = "/user/password", method = RequestMethod.GET)
	public String userPasswordUI() {
		return "/user/password";
	}
	
	/**
	 * 用户修改密码
	 */
	@RequestMapping(value = "/user/password", method = RequestMethod.POST)
	public String userPassword(HttpServletRequest request, HttpServletResponse response,String password ,String newPassword) {
		
		String username = request.getUserPrincipal().getName();
		User user = this.getUserDao().getUserByUsername(username, password);
		if(user.getUsername() == null && user.getPassword() == null){
			request.setAttribute("inputerror", "提示信息：密码错误");
			return "/user/password";
		}
		
		this.getUserDao().updatePassword(newPassword, user.getId());
		request.setAttribute("inputerror", "提示信息：密码修改成功");
		return "/user/password";
	}
	
	
}
